package org.darkforest.merchant.controller;

import org.darkforest.common.services.BundlingService;
import org.darkforest.common.util.Utils;
import org.darkforest.merchant.service.IGoodsService;
import org.darkforest.model.GoodSearchBean;
import org.darkforest.model.PBundling;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Administrator on 2016/1/13.
 */
@Controller
@RequestMapping(value = "promotion")
public class DiscountPackageController {


    @Autowired
    private BundlingService bundlingService;


    @Autowired
    private IGoodsService goodsService;

    /**
     * 促销套餐分页列表
     * @param session
     * @param p
     * @param state
     * @param blName
     * @return
     */
    @RequestMapping(value = "discountPackageList")
    public ModelAndView discountPackageList(HttpSession session, @RequestParam(required = false, defaultValue = "1") String p,
                                            @RequestParam(required = false, defaultValue = "") String state,
                                            @RequestParam(required = false, defaultValue = "") String blName) {
        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();

        ModelAndView res = new ModelAndView("promotion/bundling/bundling_list");
        PBundling pBundling = new PBundling();
        pBundling.setStart(Integer.parseInt(p));
        pBundling.setStoreId(storeId);
        pBundling.setBlState("".equals(state) ? null : new Integer(state));
        pBundling.setBlName("".equals(blName) ? null : blName);
        pBundling.setResultList(bundlingService.findPBundlingListWithGoodsByCondition(pBundling));
        res.addObject("pageBean", pBundling);
        return res;
    }


    /**
     * 跳转到添加促销商品套餐
     * @param session
     * @return
     */
    @RequestMapping(value = "toAddDiscountPackage")
    public ModelAndView toAddDiscountPackage(HttpSession session){
        Integer storeId = ((StoreSellerVo) session.getAttribute("sellerInfo")).getStoreId();
        //判断促销套餐是否超过50个 如果超过50个 则不可以再继续添加
        List<PBundling> pBundlingList = bundlingService.findListByCondition(new PBundling(storeId,false));
        ModelAndView res;
        if(pBundlingList.size()<=50){
            res = new ModelAndView("promotion/bundling/bundling_add");
        }else{
            res = new ModelAndView("");
        }

        return  res;
    }


    /**
     * 新增优惠套餐
     * @param pBundling
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "doAddDiscountPackage")
    public @ResponseBody AjaxReturnData doAddDiscountPackage(PBundling pBundling,HttpSession session) throws Exception {
        StoreSellerVo sellerInfo = (StoreSellerVo) session.getAttribute("sellerInfo");
        pBundling.setStoreId(sellerInfo.getStoreId());
        pBundling.setStoreName(sellerInfo.getStoreName());

        AjaxReturnData ajaxReturnData = bundlingService.addDiscountPackage(pBundling);
        return ajaxReturnData;
    }


    /**
     * 跳转到管理优惠套餐页面
     * @param blId
     * @return
     */
    @RequestMapping(value = "toManagerBl")
    public ModelAndView toManagerBl(Integer blId){
        ModelAndView res = new ModelAndView("promotion/bundling/bundling_manager");
        res.addObject("bl",bundlingService.getBundlingWithGoodsById(blId,null));
        return  res;
    }


    /**
     * 保存编辑后的优惠套餐
     * @param pBundling
     * @param session
     * @return
     */
    @RequestMapping(value = "doModifyBundling")
    public @ResponseBody AjaxReturnData doModifyBundling(PBundling pBundling,HttpSession session) throws Exception {
        AjaxReturnData ajaxReturnData = bundlingService.doModifyBundling(pBundling);
        return ajaxReturnData;
    }


    /**
     * 批量删除优惠套装
     * @param ids
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "delBundlingBatch")
    public @ResponseBody AjaxReturnData delBundlingBatch(Integer[] ids,HttpSession session) throws Exception {
//        StoreSellerVo sellerInfo = (StoreSellerVo) session.getAttribute("sellerInfo");
        String url = "discountPackageList";
        AjaxReturnData ajaxReturnData = bundlingService.delBundlingBatch(ids,url);
        return ajaxReturnData;
    }



    /**
     * 为添加优惠套餐选择商品
     * @param request
     * @param goodsName
     * @param start
     * @return
     */
    @RequestMapping(value = "chooseBundlingGoods")
    public ModelAndView chooseBundlingGoods(HttpServletRequest request,
                                           @RequestParam(required = false, defaultValue = "") String goodsName,
                                           @RequestParam(required = false, defaultValue = "1") String start
    ) throws UnsupportedEncodingException {
        ModelAndView modelAndView = new ModelAndView("promotion/bundling/choose_bundling_goods");
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

        GoodSearchBean goodSearchBean = new GoodSearchBean();
        goodSearchBean.setGoodsName(new String(goodsName.getBytes("ISO-8859-1"), "UTF-8"));
        goodSearchBean.setStoreId(sellerVo.getStoreId());
        goodSearchBean.setStart(Integer.parseInt(start));
        goodSearchBean.setPageSize(8);
        goodSearchBean.setGoodsType(0);
        goodSearchBean.setGoodsState(1);
        goodSearchBean.setGoodsVerify(1);
        this.goodsService.searchGoodsByPage(goodSearchBean);
        modelAndView.addObject("pageBean",goodSearchBean);
        return modelAndView;
    }

}
